package com.zyf.pattern.strategy.pay.payport;

import java.util.HashMap;
import java.util.Map;

/**
 * 使用工厂方法的方式实现策略
 * create by yifeng
 */
public class PayStrategy {
    /*
        ALIPAY(new AliPay()),
    WECHAT_PAY(new WechatPay()),
    UNION_PAY(new UnionPay());
     */
    public static final String ALI_PAY = "AliPay";
    public static final String WECHAT_PAY = "WechatPay";
    public static final String UNION_PAY = "UnionPay";
    // 提供默认的支付方式
    public static final String DEFAULT_PAY = ALI_PAY;

    private static Map<String, Payment> payStrategyList = new HashMap<>();

    // 饿汉式加载,拿到各种支付的引用，此处类似静态代理
    static {
        payStrategyList.put(ALI_PAY, new AliPay());
        payStrategyList.put(WECHAT_PAY, new WechatPay());
        payStrategyList.put(UNION_PAY, new UnionPay());
    }

    public static Payment get(String payKey) {
        if (!payStrategyList.containsKey(payKey)) {
            return payStrategyList.get(DEFAULT_PAY);
        }
        return payStrategyList.get(payKey);
    }

}
